/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.datetime;

import org.openaecfm.model.datetime.DateTime;
import org.openaecfm.model.datetime.EventTime;
import org.openaecfm.model.datetime.SchedulingTime;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class EventTimeIO extends
		AbstractEntityInstanceInputImpl<EventTime> implements
		IEntityInstanceOutput<EventTime> {

	private IEntityInstanceUtilOutput<SchedulingTime> utilOutput;
	private IEntityInstanceUtilInput<SchedulingTime> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<SchedulingTime> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<SchedulingTime> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(EventTime pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCEVENTTIME",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set ActualDate
		if (pojo.getActualDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(
					pojo.getActualDate().toString(), StringParameter.UTF_8));
		}

		// Set EarlyDate
		if (pojo.getEarlyDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getEarlyDate().toString(),
					StringParameter.UTF_8));
		}

		// Set LateDate
		if (pojo.getLateDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getLateDate().toString(),
					StringParameter.UTF_8));
		}

		// Set ScheduleDate
		if (pojo.getScheduleDate() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getScheduleDate()
					.toString(), StringParameter.UTF_8));
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, EventTime pojo,
			IAppDataManager appDataManager) {
		return;
	}

	@Override
	protected EventTime doCreateObject() {
		return new EventTime();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			EventTime pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ActualDate
		Parameter actualDate = sei.getParameter(3);
		if (actualDate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) actualDate;
			pojo.setActualDate(new DateTime(s.toUTF8()));
		}

		// Set EarlyDate
		Parameter earlyDate = sei.getParameter(4);
		if (earlyDate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) earlyDate;
			pojo.setEarlyDate(new DateTime(s.toUTF8()));
		}

		// Set LateDate
		Parameter lateDate = sei.getParameter(5);
		if (lateDate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) lateDate;
			pojo.setLateDate(new DateTime(s.toUTF8()));
		}

		// Set ScheduleDate
		Parameter scheduleDate = sei.getParameter(6);
		if (scheduleDate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) scheduleDate;
			pojo.setScheduleDate(new DateTime(s.toUTF8()));
		}
	}
}
